package com.qf.user.controller;


import com.qf.common.core.entity.User;
import com.qf.common.core.utils.JwtUtils;
import com.qf.common.core.utils.R;
import com.qf.common.core.utils.UserUtils;
import com.qf.user.service.UserService;
import com.qf.user.vo.MyVO;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zdl
 * @date 2025/3/24
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 获取我的 页面需要展示的信息
     * @return
     */
    @GetMapping("/my")
    public R<MyVO> myInfo(){
//        String token = request.getHeader("Token");
//        // 根据token 获取用户信息
//        Claims claims = JwtUtils.parseJWT(token);
//        Integer uid = claims.get("uid", Integer.class);
        Integer uid = UserUtils.getUser();
        MyVO myVO = userService.myInfo(uid);
        return R.ok(myVO);
    }

    /**
     * 上传头像
     * @param file
     * @return
     */
    @PostMapping("/avatar/upload")
    public R<?> uploadAvatar(MultipartFile file){
       //String ossPath = userService.uploadAvatar(file);
       String minioPath = userService.uploadAvatar2(file);
        return R.ok(minioPath);
    }
    @GetMapping("/info")
    public R<User> info(){
        User user = userService.getInfo();
        return R.ok(user);
    }

    @PostMapping("/update")
    public R<Integer> update(@RequestBody User user){
       int i = userService.update(user);
        return R.ok(i);
    }

}
